import serial
import serial.tools.list_ports
import math
import struct

def get_crc8(data: bytes) -> bytes:
    """
    计算CRC8校验值，多项式0x8C
    :param data: 需要校验的字节数据
    :return: CRC8校验值（0~255）
    """
    crc = 0
    for byte in data:
        crc ^= byte
        for _ in range(8):
            if crc & 0x01:
                crc = (crc >> 1) ^ 0x8C
            else:
                crc >>= 1
            crc &= 0xFF  # 保证crc始终为8位
    crc = struct.pack('<h', crc)
    return crc


begin = bytes([0x55, 0xaa])
end = bytes([0x0d, 0x0a])
angles = [1.0892242055348749, -0.17332101096690133, 1.0859638345521907, -0.00022042666860048007, 1.8829263017972695, -0.48260819561700136]
for i in range(len(angles)):
    angles[i] = int(angles[i] * 1000)  # 将角度转换为弧度制并乘以1000

# angles = [int(math.pi / 4 * 1000)] * 6  # 六个关节的角度
print(angles)
print(len(angles))
flag = 1  # 标志位
# 六个关节的角度(弧度制) *1000, short类型
len= bytes([13])  # 长度为13字节
medium_data = bytes()
flag =bytes([flag])  # 标志位
for angle in angles:
    medium_data += struct.pack('<h', angle) 

crc = get_crc8(medium_data)

out_data = begin +len+ medium_data +flag+ crc + end
print(out_data)

